package com.wenbai.servicelightluxurystickers.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;

@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void incr(String key) {
        redisTemplate.opsForValue().increment(key);
    }

    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public Integer getInt(String key) {
        Object val = redisTemplate.opsForValue().get(key);
        return val == null ? null : Integer.parseInt(val.toString());
    }

    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    // 24h过期
    public void expire(String key, int hours) {
        redisTemplate.expire(key, Duration.ofHours(hours));
    }
}
